# -*- coding: utf-8 -*-
# Create your views here.
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotFound
from django.shortcuts import render_to_response
from gpxe.menubuilder.models import Machine, UbuntuPreseed, Menu, Tool

path = "http://cobratechtools.s3.amazonaws.com/"

def index(request):
  return HttpResponse("Index..Coming soon!")

def files(request, mac, files):
  return HttpResponseRedirect(path + files)
  
def tools(request, mac, tool):
  #Case sensitive right now
  user = getUser(mac)
  try:  
    if (user != None):
      tool = Tool.objects.get(name=tool, owner=user)
    else:
      tool = Tool.objects.get(name=tool, owner=public)
  except Tool.DoesNotExist:
    return HttpResponseNotFound
  return HttpResponseRedirect(thisTool.path)
  
def console(request):
  machine_list = Machine.objects.filter(name=user)
  tool_list = 
  menu_list = 
  preseed_list = 
  
  return render_to_response('console.html')
  
def preseeds(request, mac, preseed):
  user = getUser(mac)
  try:
    if (user != None):
      preseed = UbuntuPreseed.objects.get(name=preseed, owner=user)
    else:
      preseed = UbuntuPreseed.objects.get(name=preseed, owner=public)
  except Preseed.DoesNotExist:
    return HttpResponseNotFound
    
  response = { 'locale': preseed.locale,
  'keyboard_code': preseed.keyboard_code,
  #'keyboard_code_variant': preseed.keyboard_code_variant,
  'clock_utc': preseed.clock_utc,
  'clock_timezone': preseed.clock_timezone,
  'partition_method': preseed.partition_method,
  'partition_recipe': preseed.partition_recipe,
  'partition_filesystem': preseed.partition_filesystem,	
  'user_full_name': preseed.user_full_name,
  'user_name': preseed.user_name,
  'user_crypted_password': preseed.user_crypted_password,
  'encrypt_home': preseed.encrypt_home,
  'tasksel': preseed.tasksel,
  'upgrade_packages': preseed.upgrade_packages,
  'final_script': preseed.final_script }
  
  return render_to_response('preseed.cfg', response, mimetype='text/plain')
  
def menu(request, mac, menu):
  user = getUser(mac)
  try:  
    if (user != None):
      menu = Menu.objects.get(title=menu, owner=user)
    else:
      menu = Menu.objects.get(title=menu, owner=public)
  except Menu.DoesNotExist:
    return HttpResponseNotFound
  
  if (menu.prompt):
    prompt = 0
  else:
    prompt = 1
    
  response = { 'default': menu.menu_file,
  'prompt': prompt,
  'timeout': menu.timeout }
  
  return render_to_response('template.menu', response, mimetype='text/plain')

def gpxe(request):
  return HttpResponse('<p>#!gpxe </p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p><p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo</p> <p>echo Welcome to CobraTech Tools </p> <p>echo</p> <p>echo Connecting to the server</p> <p></p> <p>dhcp net0</p> <p>set 209:string file/${net0/mac}/pxelinux.cfg/default</p> <p>set 210:string http://128.104.196.44.com/</p> <p>chain ${210:string}pxelinux.0</p>')

#Auxillary functions
def getUser(mac):
  try:
    #Check if this machine already has an owner
    user = Machine.objects.get(identifier=mac).owner
  except Machine.DoesNotExist:
    user = None
  return user

  
  
  